package troll.client;

import javax.swing.ComboBoxEditor;

import troll.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.ListBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Trolls implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		// Panels
		final VerticalPanel vPanel = new VerticalPanel();
		vPanel.setHorizontalAlignment(vPanel.ALIGN_CENTER);
		vPanel.setVerticalAlignment(vPanel.ALIGN_MIDDLE);
		final TabPanel tabPanel = new TabPanel();
		tabPanel.setWidth("960px");
		final FlowPanel onglet1 = new FlowPanel();
		final FlowPanel onglet2 = new FlowPanel();
		
		// Trolls
		final Label labelTrollRace = new Label("Sélectionnez une race");
		
		// -------------- List --------------
		final ListBox listRace = new ListBox();
		listRace.addItem("test1");
		listRace.addItem("test2");
		listRace.addItem("test3");
		
		// -------------- TextBox --------------
		final TextBox trollNameTextBox = new TextBox();
		
		final TextBox textBoxAttaque = new TextBox();
		textBoxAttaque.setWidth("25px");
		textBoxAttaque.setEnabled(false);
		textBoxAttaque.setValue("0");
		
		final TextBox textBoxDegats = new TextBox();
		textBoxDegats.setWidth("25px");
		textBoxDegats.setEnabled(false);
		textBoxDegats.setValue("0");
		
		final TextBox textBoxEsquive = new TextBox();
		textBoxEsquive.setWidth("25px");
		textBoxEsquive.setEnabled(false);
		textBoxEsquive.setValue("0");
		
		final TextBox textBoxRegen = new TextBox();
		textBoxRegen.setWidth("25px");
		textBoxRegen.setEnabled(false);
		textBoxRegen.setValue("0");
		
		final TextBox textBoxPv = new TextBox();
		textBoxPv.setWidth("25px");
		textBoxPv.setEnabled(false);
		textBoxPv.setValue("0");
		
		// -------------- Boutons --------------
		final Button buttonCreateTroll = new Button("Créer");
		final Button buttonPlusAttaque = new Button("+");
		final Button buttonMoinsAttaque = new Button("-");
		
		final Button buttonPlusDegats = new Button("+");
		final Button buttonMoinsDegats = new Button("-");
		
		final Button buttonPlusEsquive = new Button("+");
		final Button buttonMoinsEsquive = new Button("-");
		
		final Button buttonPlusRegen = new Button("+");
		final Button buttonMoinsRegen = new Button("-");
		
		final Button buttonPlusPv = new Button("+");
		final Button buttonMoinsPv = new Button("-");
		
		// -------------- Labels --------------
		final Label trollNameLabel = new Label("Entrez le nom du troll");
		final Label labelAttaque = new Label("Attaque");
		final Label labelDegats = new Label("Dégâts");
		final Label labelEsquive = new Label("Esquive");
		final Label labelRegen = new Label("Regénération");
		final Label labelPv = new Label("Points de vie");
		
		final Label labelMaxCompetences = new Label("50");
		
		final Label test = new Label("FUUUUUUU");
		
		// -------------- RootPanel --------------
		onglet1.add(labelTrollRace);
		onglet1.add(listRace);
		onglet1.add(trollNameLabel);
		onglet1.add(trollNameTextBox);
		
		onglet1.add(labelMaxCompetences);
		
		// Attaque
		onglet1.add(labelAttaque);
		onglet1.add(textBoxAttaque);
		onglet1.add(buttonPlusAttaque);
		onglet1.add(buttonMoinsAttaque);
		
		// Dégâts
		onglet1.add(labelDegats);
		onglet1.add(textBoxDegats);
		onglet1.add(buttonPlusDegats);
		onglet1.add(buttonMoinsDegats);
		
		// Esquive
		onglet1.add(labelEsquive);
		onglet1.add(textBoxEsquive);
		onglet1.add(buttonPlusEsquive);
		onglet1.add(buttonMoinsEsquive);
		
		// Régénération
		onglet1.add(labelRegen);
		onglet1.add(textBoxRegen);
		onglet1.add(buttonPlusRegen);
		onglet1.add(buttonMoinsRegen);
		
		// Points de vie
		onglet1.add(labelPv);
		onglet1.add(textBoxPv);
		onglet1.add(buttonPlusPv);
		onglet1.add(buttonMoinsPv);
		
		tabPanel.add(onglet1, "Créer un troll");
		tabPanel.add(onglet2, "Onglet2");
		
		vPanel.add(tabPanel);
		RootPanel.get().add(vPanel);
		
		tabPanel.selectTab(0);
		
		//RootPanel.get().add(buttonCreateTroll);
		
		// -------------- Handlers --------------
		// Attaque
		buttonPlusAttaque.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				incrementerTextBox(textBoxAttaque, labelMaxCompetences);
			}
		});
		
		buttonMoinsAttaque.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				decrementerTextBox(textBoxAttaque, labelMaxCompetences);
			}
		});
		
		// Dégâts
		buttonPlusDegats.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				incrementerTextBox(textBoxDegats, labelMaxCompetences);
			}
		});
		
		buttonMoinsDegats.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				decrementerTextBox(textBoxDegats, labelMaxCompetences);
			}
		});
		
		// Esquive
		buttonPlusEsquive.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				incrementerTextBox(textBoxEsquive, labelMaxCompetences);
			}
		});
		
		buttonMoinsEsquive.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				decrementerTextBox(textBoxEsquive, labelMaxCompetences);
			}
		});
		
		// Regen
		buttonPlusRegen.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				incrementerTextBox(textBoxRegen, labelMaxCompetences);
			}
		});
		
		buttonMoinsRegen.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				decrementerTextBox(textBoxRegen, labelMaxCompetences);
			}
		});
		
		// Pv
		buttonPlusPv.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				incrementerTextBox(textBoxPv, labelMaxCompetences);
			}
		});
		
		buttonMoinsPv.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				decrementerTextBox(textBoxPv, labelMaxCompetences);
			}
		});
		// -------------- FIN --------------
		
		final Button sendButton = new Button("Send");
		final TextBox nameField = new TextBox();
		nameField.setText("GWT User");
		final Label errorLabel = new Label();

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				GetFileContent();
				//sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
				greetingService.greetServer(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								closeButton.setFocus(true);
							}
						});
			}
		
		
		/**
		 * Send the name from the nameField to the server and wait for a response.
		 */
		private void GetFileContent() {
			
			// Then, we send the input to the server.
			sendButton.setEnabled(false);
			greetingService.callFileReading("race", 
					new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
							dialogBox.setText("Remote Procedure Call (FileReading) - Failure");
							serverResponseLabel.addStyleName("serverResponseLabelError");
							serverResponseLabel.setHTML(SERVER_ERROR);
							dialogBox.center();
							closeButton.setFocus(true);
						}

						public void onSuccess(String result) {
							dialogBox.setText("YEEEEEEEEEEP");
							serverResponseLabel.removeStyleName("serverResponseLabelError");
							serverResponseLabel.setHTML(result);
							dialogBox.center();
							closeButton.setFocus(true);
						}
					});
		}
	}

		

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	}



	
	
	
	public void incrementerTextBox(TextBox tb, Label competencesMax) {
		int maxComp = Integer.parseInt(competencesMax.getText());
		if (maxComp != 0) {
			int value = Integer.parseInt(tb.getValue())+1;
			tb.setValue(String.valueOf(value));
			maxComp--;
			
			competencesMax.setText(String.valueOf(maxComp));
		}
	}
	
	public void decrementerTextBox(TextBox tb, Label competencesMax) {
		int maxComp = Integer.parseInt(competencesMax.getText());
		int value = Integer.parseInt(tb.getValue());
		if (maxComp <= 50 && value > 0) {
			value--;
			tb.setValue(String.valueOf(value));
			maxComp++;
			
			competencesMax.setText(String.valueOf(maxComp));
		}
	}
}
